using Microsoft.AspNetCore.Mvc;
using System;
using Volo.Abp.AspNetCore.Mvc;
using WorkFlowCore.Framework.Utils;
using WorkFlowCore.Host.ViewModels.DevTools;

namespace WorkFlowCore.Host.Controllers
{
    /// <summary>
    /// 开发工具控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class DevToolsController : AbpControllerBase
    {
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="request">加密请求参数</param>
        /// <returns>加密后的Base64字符串</returns>
        [HttpPost("Encrypt")]
        public string Encrypt([FromBody] CryptoRequestDto request)
        {
            if (string.IsNullOrEmpty(request.PlainText) || string.IsNullOrEmpty(request.Key))
            {
                throw new ArgumentException("参数不能为空");
            }

            if (request.Key.Length != 16 && request.Key.Length != 24 && request.Key.Length != 32)
            {
                throw new ArgumentException("密钥长度必须为16、24或32字节");
            }

            return AESHelper.Encrypt(request.PlainText, request.Key, request.Key);
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="request">解密请求参数</param>
        /// <returns>解密后的原始字符串</returns>
        [HttpPost("Decrypt")]
        public string Decrypt([FromBody] CryptoRequestDto request)
        {
            if (string.IsNullOrEmpty(request.PlainText) || string.IsNullOrEmpty(request.Key))
            {
                throw new ArgumentException("参数不能为空");
            }

            if (request.Key.Length != 16 && request.Key.Length != 24 && request.Key.Length != 32)
            {
                throw new ArgumentException("密钥长度必须为16、24或32字节");
            }

            return AESHelper.Decrypt(request.PlainText, request.Key, request.Key);
        }
    }
}
